﻿#pragma once 
#include "common.h"
#include <QDesktopWidget>
#include "qpainter.h"
#include "qcoreevent.h"
#include "EagleDefine.h"
#include "qcoreevent.h"
#include "qapplication.h"
#include "QDateTime"
#include "QTextStream"
#include "EagleDeviceMgr.h"
#include "EagleMainFrame.h"
#include <QDir>
#include <QMessageBox>

QEvent::Type MyEventType = (QEvent::Type)QEvent::registerEventType(QEvent::User + 100);
QEvent::Type UiEventType = (QEvent::Type)QEvent::registerEventType(QEvent::User + 101);
QStringList  g_strListDlgTiltle = QStringList() 
	<< QObject::tr("导出") << QObject::tr("数据自检") << QObject::tr("异常恢复数据扫描");


StaticFrameMgr::StaticFrameMgr(QString strMidTxt)
{
	m_strNam = strMidTxt;
	QRect screen = QApplication::desktop()->screenGeometry();

#ifndef CHENGGS_VERSION
	screen = QRect(0,0,1024, 1024);
#endif

	DefaultStaticData.line = screen.height();
	DefaultStaticData.column = screen.width();
	DefaultStaticData.decoded_column = screen.width();
	DefaultStaticData.decoded_line = screen.height();
	DefaultStaticData.read_format_column = screen.width();
	DefaultStaticData.read_format_line = screen.height();
	DefaultStaticData.color_depth = 8;
	DefaultStaticData.real_color_depth = 8;
	DefaultStaticData.displayed = false;
	DefaultStaticData.frame_type = FRAME_TYPE_DEFAULT;
	DefaultStaticData.orig_data_len = DefaultStaticData.line * DefaultStaticData.column;
	DefaultStaticData.decoded_buf_len = DefaultStaticData.line * DefaultStaticData.column * 3;
	DefaultStaticData.orig_data = new unsigned char[DefaultStaticData.orig_data_len];
	DefaultStaticData.read_format_data = new unsigned char[DefaultStaticData.orig_data_len];
	DefaultStaticData.buf = new  unsigned char[DefaultStaticData.decoded_buf_len];
	memset(DefaultStaticData.orig_data, 0, DefaultStaticData.orig_data_len);
	memset(DefaultStaticData.read_format_data, 0, DefaultStaticData.orig_data_len);
	memset(DefaultStaticData.buf, 0, DefaultStaticData.decoded_buf_len);

	CurrentStaticData.line = 0;
	CurrentStaticData.column = 0;
	CurrentStaticData.color_depth = 0;
	CurrentStaticData.real_color_depth = 0;
	CurrentStaticData.displayed = false;
	CurrentStaticData.frame_type = FRAME_TYPE_STATIC;
	CurrentStaticData.decoded_buf_len = 0;
	CurrentStaticData.orig_data_len = 0;
	CurrentStaticData.orig_data = NULL;
	CurrentStaticData.read_format_data = NULL;
	CurrentStaticData.buf = NULL;
}

DisplayFrameData & StaticFrameMgr::GetDefaultData()
{
	memset(DefaultStaticData.orig_data, 0, DefaultStaticData.orig_data_len);
	memset(DefaultStaticData.read_format_data, 0, DefaultStaticData.orig_data_len);
	memset(DefaultStaticData.buf, 0, DefaultStaticData.decoded_buf_len);
	QImage& image = GetDefaultImage();
	memcpy(DefaultStaticData.buf, image.constBits(), image.byteCount());

	return DefaultStaticData;
}

QImage& StaticFrameMgr::GetDefaultImage()
{
	if (!m_defaultImg.isNull())
	{
		return m_defaultImg;
	}

	QPainter painter;
	QRect screenrect = QApplication::desktop()->screenGeometry();
	int w = screenrect.width(), h = screenrect.height();
	QImage image(w, h, QImage::Format_RGB888);
	image.fill(Qt::black);
	painter.begin(&image);
	QFont font = painter.font();
	font.setPixelSize(16);
	font.setWeight(QFont::Bold);
	painter.setFont(font);
	QPen pen = painter.pen();
	pen.setColor(Qt::white);
	painter.setPen(pen);
	//pen.setStyle(Qt::DashDotLine);
	QRect boundingRect;
	//
	painter.drawText(QRect(0, 0, w, h), Qt::AlignCenter, m_strNam, &boundingRect);
	pen.setColor(Qt::darkBlue);
	font.setPixelSize(12);
	painter.setPen(pen);
	painter.setFont(font);
	//
	painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width()));
	painter.end();

	return m_defaultImg = image;
}


void ReplayContext::GenerateTimeToIndexMap()
{
	if (replay_mothod != 0 && frame_time_interval != 0)
	{
		EagleData_CcdRecord *_ccd = get_related_ccd_rec();
		long current_index = 0;
		for (long index = replay_start_index; index <= replay_end_index; index++)
		{
			unsigned long h = 0, m = 0, s = 0, ms = 0;
			GetRecordTimeInIndex(_ccd->record_id, _ccd->id, index, _ccd->column, _ccd->line, _ccd->color_depth, 
				EagleConfigFile::getInstance()->time_decoder_format.toStdWString(), &h, &m, &s, &ms);
			if (ms % frame_time_interval == 0)
			{
				time_to_index[current_index++] = index;
			}
		}
	}
	else
	{
		for (long index = replay_start_index; index <= replay_end_index; index++)
		{
			time_to_index[index] = index;
		}
	}

	auto it = time_to_index.begin();
	current_frame_index = replay_start_index = it->first;
	it = time_to_index.end();
	it--;
	replay_end_index = it->first;
}


//////////////////////////////////////////////////////////////////////////
// common implict [11/4/2016 ejitec]

int EagleLibReadOneStoreFrameAndDecode(IN EagleData_Record_Id rec_id,
	IN EagleData_CcdRecord_Id ccd_rec_id,
	IN long frame_index,
	IN DisplayFrameData* frame_data,
	IN std::wstring read_format_name,
	IN bool need_decoder,
	IN std::wstring decoder_name,
	IN DecodeRequestStruct* decode_para)
{
	int success = -1;
	static QTime tm;
	//tm.start();
	success = EagleData_ReadOneStoredFrame(rec_id, ccd_rec_id, frame_index,
		(char*)frame_data->orig_data, frame_data->orig_data_len,
		(char*)frame_data->frame_head, frame_data->frame_head_len);
	//std::cout << "EagleData_ReadOneStoredFrame QTime:" << tm.elapsed() << endl;
	std::wstring time_format = EagleConfigFile::getInstance()->time_decoder_format.toStdWString();
	if (success == 0)
	{
		FrameHead head;

#ifdef XIANGS_VERSION
		unsigned char head_buf[512] = { 0 };
		int nFrmOffset = 0;
		int nTmOffset = 0;
		int nAEPos = 0;
		memcpy(&nTmOffset, decode_para->Reserved, 4); 
		memcpy(&nAEPos, decode_para->Reserved + 4, 4);
		memcpy(&nFrmOffset, decode_para->Reserved + 8, 4);
		EagleData_Read512InsideFrame(rec_id, ccd_rec_id, frame_index + nFrmOffset, 0, (char*)head_buf);
		EagleData_AnalyzeFrameHead((char*)head_buf, &head);
// 		memcpy(head_buf + sizeof(XIAN_GS_DATA), &nTmOffset, 4);
// 		memcpy(head_buf + sizeof(XIAN_GS_DATA)+4, &nAEPos, 4);
// 		memcpy(frame_data->frame_head, head_buf, 512);
        memcpy(head_buf + (512 - 8), &nTmOffset, 4);
        memcpy(head_buf + (512 - 4), &nAEPos, 4);
        memcpy(frame_data->frame_head, head_buf, 512);
#else
		EagleData_AnalyzeFrameHead((char*)frame_data->frame_head, &head);
#endif
		frame_data->displayed = false;
		frame_data->rotated = false;
		frame_data->line = decode_para->original_line;
		frame_data->column = decode_para->original_column;
		frame_data->color_depth = decode_para->original_color_depth;
		frame_data->real_color_depth = decode_para->original_real_color_depth;
		unsigned long h = 0, m = 0, s = 0, ms = 0;
		GetRecordTimeFromReadBuffer(frame_data->orig_data, frame_data->frame_head, frame_data->column, frame_data->line,
			frame_data->color_depth, time_format,
			&h, &m, &s, &ms);
		frame_data->h = h;
		frame_data->m = m;
		frame_data->s = s;
		frame_data->ms = ms;
		//frame.frame_index = ((unsigned int)head.frame_index_high) << 16 | head.frame_index_low;
		frame_data->frame_index = frame_index;
		frame_data->tag = head.tag;
		//tm.start();
		//调整readout format
		DecoderPluginMgr::getInstance()->getReadFormat(read_format_name)->Decode(
			frame_data->read_format_data,
			frame_data->orig_data_len,
			frame_data->orig_data,
			frame_data->color_depth,
			frame_data->column,
			frame_data->line,
			decode_para
			);
		//std::wcout << L"getReadFormat(" << read_format_name << L")->Decode QTime:" << tm.elapsed() << endl;
		DecoderPlugin::DecodedParam read_format_param(frame_data->color_depth, frame_data->column, frame_data->line);
		DecoderPluginMgr::getInstance()->getReadFormat(read_format_name)->GetDecodedParam(
			read_format_param,
			frame_data->read_format_color_depth,
			frame_data->read_format_column,
			frame_data->read_format_line,
			decode_para
			);
		//if need decode
		if (need_decoder)
		{
			//tm.start();
			int rotated = DecoderPluginMgr::getInstance()->getDecoder(decoder_name)->Decode(
				frame_data->buf,
				frame_data->decoded_buf_len,
				frame_data->read_format_data,
				frame_data->read_format_color_depth,
				frame_data->read_format_column,
				frame_data->read_format_line,
				decode_para
				);
			//std::wcout <<  L"getDecoder(" << decoder_name << L")->Decode QTime:" << tm.elapsed() << endl;
			DecoderPlugin::DecodedParam decoded_param(frame_data->read_format_color_depth, frame_data->read_format_column, frame_data->read_format_line);
			DecoderPluginMgr::getInstance()->getDecoder(decoder_name)->GetDecodedParam(
				decoded_param,
				frame_data->decoded_color_depth,
				frame_data->decoded_column,
				frame_data->decoded_line,
				decode_para
				);
			frame_data->rotated = (bool)rotated;
		}
	}
	else
	{
		qDebug() << "EagleData_ReadOneStoredFrame,ret:" << success;
	}
	return success;
}



bool SaveImageFile(char *data, int column, int line, int type, const QString &name)
{
	bool res = false;
	QImage image((unsigned char *)data,column, line,QImage::Format_RGB888);
	if (!image.isNull()){
		if (type == EExportBmp) {
			res = image.save(name, "bmp");
		}
		else if (type == EExportJpeg) {
			res = image.save(name, "jpg",85);
		}
		else if (type == EExportTiff) {
			res = image.save(name, "tif");
		}
		else
		{
			;
		}
	}
	return res;
}

bool SaveImageFile(QImage& image, int type, const QString& name)
{
	bool res = false;
	if (!image.isNull()){
		if (type == EExportBmp) {
			res = image.save(name, "bmp");
		}
		else if (type == EExportJpeg) {
			res = image.save(name, "jpg", 85);
		}
		else if (type == EExportTiff) {
			res = image.save(name, "tif");
		}
		else
		{
			;
		}
	}

	return res;
}

#if (defined CHENGGS_VERSION && defined _WIN32)
bool SaveRawFile(char *data, int size, const QString& name)
{
	HANDLE hFile = CreateFile(name.toStdWString().c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
		return false;
	DWORD dwWriteLen = 0;
	WriteFile(hFile, data, size, &dwWriteLen, NULL);
	CloseHandle(hFile);
	return dwWriteLen == size;
}
#else
bool SaveRawFile(char *data, int size, const QString& name)
{

#ifdef _WIN32

	HANDLE hFile = CreateFile(name.toStdWString().c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
		CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
		return false;
	DWORD dwWriteLen = 0;
	WriteFile(hFile, data, size, &dwWriteLen, NULL);
	CloseHandle(hFile);

	return dwWriteLen == size;

#else
	std::ofstream ofs_data;
	ofs_data.open(name.toStdString().c_str(), std::ofstream::out | std::ofstream::binary);
	if (ofs_data.is_open() && ofs_data.good()){
		ofs_data.write((char*)data, size);
		ofs_data.close();
		return true;
	}
	return false;

#endif


}
#endif


void ExportContext::GenerateTimeToIndexMap()
{
	if (export_mothod != 0 && frame_time_interval != 0)  // 按照时间导出
	{
		EagleData_CcdRecord *_ccd = get_related_ccd_rec();
		long current_index = 0;
		bool findHead = false;

		for (int32_t index = export_start_index; index <= export_end_index; index++)
		{
			unsigned long h = 0, m = 0, s = 0, ms = 0;
			GetRecordTimeInIndex(_ccd->record_id, _ccd->id, index, _ccd->column, _ccd->line, _ccd->color_depth, 
				EagleConfigFile::getInstance()->time_decoder_format.toStdWString(), &h, &m, &s, &ms);
			
#if (defined (CHENGGS_VERSION) && CHENGGS_VERSION >= 3)

			if (!findHead)  // 三期 需要整秒 对齐 [12/22/2016 ejitec]
			{
				findHead = (ms == 0);
				if (!findHead)
				{
					continue;
				}
			}
#endif
			if (ms % frame_time_interval == 0)
			{
				// 索引有序  值无序
				time_to_index[current_index++] = index;
			}
		}
		export_select_interval = 1;
	}
	else
	{
		for (int32_t index = export_start_index; index <= export_end_index; index++)
		{
			time_to_index[index] = index;
		}
	}

	// 避免访问越界 [12/23/2016 ejitec]
	if (time_to_index.size() == 0)
	{
		if (export_mothod != 0)
		{
			QMessageBox::warning(NULL, GetMessageBoxTitle(), QString(QObject::tr("通道:%1 找不到整秒开始帧")).arg(this->_ccd_serial));
		}
		//
		time_to_index[export_start_index] = export_start_index;
	}

	auto it = time_to_index.begin();
	current_frame_index = export_start_index = it->first;
	it = time_to_index.end();
	it--;
	export_end_index = it->first;

}

int ExportContext::check(int frame_index)
{
	long err_pix = 0;

	if (frame_index <= 1)
	{
		show_first_err_frame = true;
	}
	int ret = EagleData_CheckOneFrame(_ccdrec->record_id, _ccdrec->id, frame_index, &err_pix);
	if (ret != 0)
		return 0;

	if (err_pix != 0 && show_first_err_frame)
	{
		show_first_err_frame = false;
		ControlApiLogger(QString("").sprintf("frame: %ld, errpix = %ld", frame_index, err_pix));
	}
	return err_pix;
}

int ExportContext::check()
{
	long err_pix = 0;
	long frame_index = time_to_index.at(current_frame_index.fetch_add(export_select_interval));
	if (frame_index > export_end_index)
	{
		return 0;
	}
	if (frame_index <= 1){
		show_first_err_frame = true;
	}
	int ret = EagleData_CheckOneFrame(_ccdrec->record_id, _ccdrec->id, frame_index, &err_pix);
	if (ret != 0)
		return 0;
	if (err_pix != 0 && show_first_err_frame)
	{
		show_first_err_frame = false;
		ControlApiLogger(QString("").sprintf("frame: %ld, errpix = %ld", frame_index, err_pix));
	}
	return err_pix;
}


void WriteExportLog(BaseDisplayContext* ctx, QString strMsg)
{
	if (EagleConfigFile::getInstance()->export_log.toInt() < 2)  
	{
		return;
	}

	ExportContext* context = dynamic_cast<ExportContext*>(ctx);
	if (!context)
	{
		return;
	}

	// 导出日志
	QFile file(context->get_export_path() + QString(QDir::separator()) +QString("Ccd%1ExportLog.txt").arg(ctx->get_ccd_serial()));

	context->export_log_write_lock.lock();
	file.open(QFile::Append);
	QTextStream data(&file);
	data << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz ");
	data << strMsg;
	data << QString("\r\n");
	file.close();
	context->export_log_write_lock.unlock();
}

void CheckExportFrameIndex(ExportContext& context)
{
	long lose_frame = 0;
	long last_index = -1;
	std::sort(context.export_frame_index.begin(), context.export_frame_index.end());
	for (auto it = context.export_frame_index.begin(); it != context.export_frame_index.end(); it++){
		if ((*it != last_index + 1) && (last_index != -1))
		{
			// 帧号不连续：-2096619391 -> -2096619391
			WriteExportLog(&context, GetLogFormat(EalgeLog_Export_Log_Frame_Nonsenquence).arg(last_index).arg(*it));
			lose_frame += (*it - last_index - 1);
		}
		last_index = *it;
	}
	// 共检查119帧，丢失-537002134帧
	WriteExportLog(&context, GetLogFormat(EagleLog_Export_Log_Check_Info).arg(context.export_frame_index.size()).arg(lose_frame));
}

// 列表框 [11/15/2016 ejitec]
QStringList GenerateDeviceRow(const eagle_system_status* status, int device_id, EDeviceType device_type)
{
	QStringList row;
	row << (QString::number(device_id));
	row << (InterfaceType[device_type]);
	row << (QString("").sprintf("%d ℃", status->fpga_tempture));

	//	std::wstringstream stream;
	QString diskvolumn;
	for (int i = 0; i < status->disk_num; i++)
	{
		diskvolumn += QString("").sprintf("【%d】%dG", i, status->remained_volume[i]);
		if (i < status->disk_num - 1)
			diskvolumn += " | ";
	}
	row << (diskvolumn);

	return row;
}

QStringList GenerateCcdRow(const eagle_capture_config* ccdconfig, unsigned char diskmap, int device_id, EDeviceType device_type)
{
	QStringList row;
	row << (QString::number(ccdconfig->camera_no));
	if (device_type == EDeviceTypeCL)
		row << (CL_Mode_Str[GetCameraMode(ccdconfig->taps, ccdconfig->color_depth)]);
	else if (device_type == EDeviceTypeSDI)
		row <<(SDI_Mode_Str[CEagleDeviceMgr::GetInstance()->GetSdiMode(device_id)]);
	int column = ccdconfig->column;
	int color_depth = ccdconfig->color_depth;

#ifdef CHENGGS_VERSION   // 

	if (ccdconfig->camera_no == 3 && CHENGGS_VERSION == 2)
	{
		column /= 2;
		color_depth = (color_depth * 3) / 2;
	}

	// 三期浮窗 [11/18/2016 ejitec]
	if (ccdconfig->camera_no == 2 && CHENGGS_VERSION == 3)
	{
		column /= 4;
		color_depth = color_depth * 3;  //  [11/11/2016 ejitec]
	}

#endif

	if (device_type == EDeviceTypeCL)
		row << (QString::number(column));
	row << (QString::number(ccdconfig->line));
	row << (QString::number(color_depth));
	if (device_type == EDeviceTypeCL)
		row << (QString::number(ccdconfig->taps));
	// CaptureStatus=就绪|预览|记录|暂停 [10/25/2016 ejitec]
	row << (CaptureStatus[ccdconfig->capture_status]);

#ifdef CHENGGS_VERSION   // 

	if (CHENGGS_VERSION == 3)
	{
		int nRet = 0;
		if (CEagleDeviceMgr::GetInstance()->IsFrebOnLine(device_id, ccdconfig->camera_no, nRet))
		{
			row.removeAt(row.size() - 1);
			row << (GetCaptureStatus(4 + nRet));
		}
	}

#endif

	//	std::wstringstream stream;
	QString disk;
	for (int i = 0; i < 8; i++)
	{
		if (0x01 & (diskmap >> i))
		{
			disk += QString::number(i);
			disk += +",";
		}
	}
	row << disk;

	return row;
}

void setGloabWndStyle()
{
	QString strStly =
		"  QTabWidget::pane { /* The tab widget frame */	"
		"      border-top: 2px solid #C2C7CB;	"
		"  }	"
		"	"
		"  QTabWidget::tab-bar {	"
		"      left: 5px; /* move to the right by 5px */	"
		"  }	"
		"	"
		"  /* Style the tab using the tab sub-control. Note that	"
		"      it reads QTabBar _not_ QTabWidget */	"
		"  QTabBar::tab {	"
		"      background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,	"
		"                                  stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,	"
		"                                  stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);	"
		"      border: 2px solid #C4C4C3;	"
		"      border-bottom-color: #C2C7CB; /* same as the pane color */	"
		"      border-top-left-radius: 4px;	"
		"      border-top-right-radius: 4px;	"
		"      min-width: 8ex;	"
		"      padding: 2px;	"
		"  }	"
		"	"
		"  QTabBar::tab:selected, QTabBar::tab:hover {	"
		"      background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,	"
		"                                  stop: 0 #fafafa, stop: 0.4 #f4f4f4,	"
		"                                  stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);	"
		"  }	"
		"	"
		"  QTabBar::tab:selected {	"
		"      border-color: #9B9B9B;	"
		"      border-bottom-color: #C2C7CB; /* same as pane color */	"
		"  }	"
		"	"
		"  QTabBar::tab:!selected {	"
		"      margin-top: 2px; /* make non-selected tabs look smaller */	"
		"  }	";

	//qApp->setStyleSheet(strStly);
}


//////////////////////////////////////////////////////////////////////////
// Log 相关 [12/2/2016 ejitec]
static void toFormatInfo(QString str,int ret)
{
	if (ret == 0)
	{
		qInfo() << str.append(QObject::tr(" 成功")); // 使用 qInfo 可以降低 耦合
	}
	else
	{
		str.append(QObject::tr(" 失败,错误码:%1"));
		str = str.arg(ret);
		qCritical() << str;
	}
	
	if (g_theApp)
	{
		emit g_theApp->evt_rawLog(ret == 0 ? QtInfoMsg : QtFatalMsg, str);
	}
}

// 
void ControlApiLogger(int ret, EagleLogIndexType index, QString str1, QString str2)
{
	QString fmt = GetLogFormat(index);

	if (!str1.isEmpty())
		fmt = fmt.arg(str1);
	if (!str2.isEmpty())
		fmt = fmt.arg(str2);
	
	return toFormatInfo(fmt, ret);
}



void ControlApiLogger(int ret, EagleLogIndexType index, int nValue)
{
	QString str = GetLogFormat(index);

	return toFormatInfo(str.arg(nValue), ret);
}

void ControlApiLogger(QString str)
{
	if (g_theApp)
	{
		emit g_theApp->evt_rawLog(QtInfoMsg, str);
	}
}


QString GetLogFormat(EagleLogIndexType index)
{
	QString strTips = "";
	
	switch (index)
	{

	// 3000 [3/29/2017 ejitec]
	case EagleLog_M_Fetching_Record_List:
		strTips = QObject::tr("正在获取记录列表，请稍候");
		break;
	case EagleLog_M_Fetching_Local_Record_List:
		strTips = QObject::tr("正在获取本地记录列表，请稍候");
		break;
	case EagleLog_M_Formating_Disks:
		strTips = QObject::tr("正在格式化磁盘，请稍候");
		break;
	case EagleLog_M_Initializing_Disks:
		strTips = QObject::tr("正在初始化磁盘，请稍候");
		break;
	case EagleLog_M_Deleting_Records:
		strTips = QObject::tr("正在删除记录，请稍候");
		break;
	case EagleLog_M_Recovering_Data:
		strTips = QObject::tr("正在恢复异常断电数据，请稍候");
		break;
	case EagleLog_M_Renaming_Task:
		strTips = QObject::tr("正在重命名任务，请稍候");
		break;
	case EagleLog_M_Connecting_Device:
		strTips = QObject::tr("正在连接设备，请稍候");
		break;
	case EagleLog_M_Exporting:
		strTips = QObject::tr("正在导出数据，请稍候");
		break;
	case EagleLog_M_Checking_Error_Rate:
		strTips = QObject::tr("正在检测数据误码率，请稍候");
		break;
	case EagleLog_M_Mounting_Disk:
		strTips = QObject::tr("正在挂载磁盘，请稍候");
		break;


	// Select

	case EagleLog_M_Select_Non_Selfcheck_Camera:
		strTips = QObject::tr("请选择自检相机");
		break;
	case EagleLog_M_Select_Non_Datacheck_Record:
		strTips = QObject::tr("请选择需要进行数据检查的记录");
		break;
	case EagleLog_M_Select_Non_Rename_Record:
		strTips = QObject::tr("请选择需要重命名的任务");
		break;
	case EagleLog_M_Select_Non_Task_Camera:
		strTips = QObject::tr("请选择需要采集的相机");
		break;
	case EagleLog_M_Select_Non_Replay_Record:
		strTips = QObject::tr("请选择需要回放的记录");
		break;
	case EagleLog_M_Select_Non_Export_Record:
		strTips = QObject::tr("请选择需要导出的记录");
		break;
	case EagleLog_M_Select_Non_Delete_Record:
		strTips = QObject::tr("请选择需要删除的记录");
		break;
	case EagleLog_M_Select_Non_Export_Dir:
		strTips = QObject::tr("请选择存储目录");
		break;
	case EagleLog_M_Select_Non_Format_Device:
		strTips = QObject::tr("请选择需要格式化的设备");
		break;
	case EagleLog_M_Select_Non_Restore_Device:
		strTips = QObject::tr("请选择需要恢复出厂设置的设备");
		break;
	case EagleLog_M_Select_Non_Operate_Device:
		strTips = QObject::tr("请选择需要操作的设备");
		break;
	
	// control
	case EagleLog_C_Format_Disks:
	{
		strTips = QObject::tr("格式化磁盘");
		break;
	}
	case EagleLog_C_Initialize_Disks:
	{
		strTips = QObject::tr("初始化磁盘");
		break;
	}
	case EagleLog_C_Restore_Factory_Setting:
	{
		strTips = QObject::tr("恢复出厂设置");
		break;
	}
	case EagleLog_C_Rename_Task:
	{
		strTips = QObject::tr("重命名 %1 为 %2");
		break;
	}
	case EagleLog_C_Start_Task:
	{
		strTips = QObject::tr("开始任务 %1");
		break;
	}
	case EagleLog_C_Stop_Task:
	{
		strTips = QObject::tr("停止任务");
		break;
	}
	case EagleLog_C_Start_Record:
	{
		strTips = QObject::tr("开始记录(%1)");
		break;
	}
	case EagleLog_C_Pause_Record:
	{
		strTips = QObject::tr("暂停记录");
		break;
	}
	case EagleLog_C_Resume_Record:
	{
		strTips = QObject::tr("继续记录");
		break;
	}
	case EagleLog_C_StopRecord:
	{
		strTips = QObject::tr("停止记录");
		break;
	}
	case EagleLog_C_Connect_Device:
	{
		strTips = QObject::tr("设置通讯方式");
		break;
	}
	case EagleLog_C_Disconnect_Device:
	{
		strTips = QObject::tr("断开设备");
		break;
	}
	case EagleLog_C_Delete_Record:
	{
		strTips = QObject::tr("删除记录 %1");
		break;
	}
	case EagleLog_C_Start_Selfcheck:
	{
		strTips = QObject::tr("开始自检");
		break;
	}
	
	case EagleLog_C_Set_Disk_Assign:
	{
		strTips = QObject::tr("设置相机磁盘对应关系");
		break;
	}
	case EagleLog_C_System_Config:
	{
		strTips = QObject::tr("设置系统配置");
		break;
	}
	case EagleLog_C_Set_Ccd_Param:
	{
		strTips = QObject::tr("设置相机 %1 采集参数");
		break;
	}
	case EagleLog_C_Recover_Data_Done:
	{
		strTips = QObject::tr("异常恢复完成");
		break;
	}
	case EagleLog_C_Evfs_Bind:
	{
		strTips = QObject::tr("绑定盘符");
		break;
	}
	case EagleLog_C_Evfs_Unbind:
	{
		strTips = QObject::tr("解绑定盘符");
		break;
	}
	case EagleLog_C_Evfs_Start_Write:
	{
		strTips = QObject::tr("开始写入");
		break;
	}
	case EagleLog_C_Evfs_Stop_Write:
	{
		strTips = QObject::tr("取消写入");
		break;
	}
	case EagleLog_C_Evfs_Write_Status:
	{
		strTips = QObject::tr("写入状态");
		break;
	}

	case EagleLog_I_Device_Separatly_Start_Task:
	{
		strTips = QObject::tr("设备已开始任务");
		break;
	}
	case EagleLog_I_Device_Separatly_Stop_Task:
	{
		strTips = QObject::tr("设备已停止任务");
		break;
	}
	case EagleLog_I_Device_Separatly_Start_Record:
	{
		strTips = QObject::tr("设备已开始记录");
		break;
	}
	case EagleLog_I_Device_Separatly_Pause_Record:
	{
		strTips = QObject::tr("设备已暂停记录");
		break;
	}
	case EagleLog_I_Device_Separatly_Resume_Record:
	{
		strTips = QObject::tr("设备已恢复记录");
		break;
	}
	case EagleLog_I_Device_Separatly_Stop_Record:
	{
		strTips = QObject::tr("设备已停止记录");
		break;
	}
	case EagleLog_I_Set_Conmunication_Method:
	{
		strTips = QObject::tr("设置通信方式为：%1");
		break;
	}

	// 37--
	case EagleLog_I_System_Start:
	{
		strTips = QObject::tr("系统启动");
		break;
	}
	case EagleLog_I_System_Exit:
	{
		strTips = QObject::tr("系统退出");
		break;
	}
	case EagleLog_I_Start_Replay:
	{
		strTips = QObject::tr("开始回放");
		break;
	}
	case EagleLog_I_Pause_Replay:
	{
		strTips = QObject::tr("暂停回放");
		break;
	}
	case EagleLog_I_Resume_Replay:
	{
		strTips = QObject::tr("继续回放");
		break;
	}
	case EagleLog_I_Stop_Replay:
	{
		strTips = QObject::tr("停止回放");
		break;
	}
	case EagleLog_I_Start_Export:
	{
		strTips = QObject::tr("开始导出");
		break;
	}
	case EagleLog_I_Finish_Export:
	{
		strTips = QObject::tr("完成导出");
		break;
	}
	case EagleLog_I_Canceled_Progress:
	{
		strTips = QObject::tr("%1 已取消");
		break;
	}
	case EagleLog_I_Lose_Frame:
	{
		strTips = QObject::tr("丢帧");
		break;
	}

	// WARN
	case EagleLog_W_Disk_Free_Space:
	{
		strTips = QObject::tr("磁盘 %1 剩余容量不足 %2 GB");
		break;
	}
	case EagleLog_W_Detect_Param_Dismatch:
	{
		strTips = QObject::tr("相机 %1 侦测参数和采集配置参数不一致");
		break;
	}

	// EXPORT
	case EalgeLog_Export_Log_Frame_Nonsenquence:
	{
		strTips = QObject::tr("帧号不连续： %1 -> %2");
		break;
	}
	case EagleLog_Export_Log_Check_Info:
	{
		strTips = QObject::tr("共检查 %1 帧，丢失 %2 帧");
		break;
	}
	case EagleLog_Export_Log_Start:
	{
		strTips = QObject::tr("相机:%1 %2 开始导出");
		break;
	}
	case EagleLog_Export_Log_Finish1:
	{
		strTips = QObject::tr("相机 %1 导出结束，共导出 %2 个文件");
		break;
	}
	case EagleLog_Export_Log_Finish2:  // 整形 使用 vsprintf 方式 failed
	{
		strTips = QObject::tr("相机 %1 导出结束，导出帧数:%2");
		break;
	}
	case EagleLog_Check_Data_Result:
	{
		strTips = QObject::tr("相机 %1 检查数据结束，总帧数:%2 总像素:%3\n误码像素:%4，误码率:%5");
		break;
	}

	// ERROR [3/29/2017 ejitec]
	case EagleLog_E_Preview:
	{
		strTips = QObject::tr("预览图像失败 帧号:%1");
		break;
	}
	case EagleLog_E_Avi_Encoder:
	{
		strTips = QObject::tr("导出avi失败 请确认已经安装 %1 视频编码库");
		break;
	}
	case EagleLog_E_Create_Export_Dir:
	{
		strTips = QObject::tr("创建导出目录失败：%1");
		break;
	}
	case EagleLog_E_Export_Failed:
	{
		strTips = QObject::tr("导出失败：%1");
		break;
	}
	case EagleLog_E_Get_NIC_List:
	{
		strTips = QObject::tr("获取网卡列表失败");
		break;
	}
	case EagleLog_E_Init_Realtime_Api:
	{
		strTips = QObject::tr("系统无法实时显示，初始化实时显示接口失败");
		break;
	}
		//  quit [4/25/2017 xystar]
	case EagleLog_M_Quit_Alarm:
	{
		strTips = QObject::tr("是否确定退出？");
		break;
	}
	case EagleLog_M_Quit_Still_Running_Alarm:
	{
		strTips = QObject::tr("请先停止采集，再退出系统");
		break;
	}
	case EagleLog_M_Rename_Local_Task_Alarm:
	{
		strTips = QObject::tr("无法重命名本地记录");
		break;
	}
	case EagleLog_M_Delete_Local_Task_Alarm:
	{
		strTips = QObject::tr("无法删除本地记录 %1");
		break;
	}
	case EagleLog_M_Record_Time_Too_Less_Alarm:
	{
		strTips = QObject::tr("记录时间过短，请设置至少10秒记录时间");
		break;
	}
	case EagleLog_M_Recover_Data_First_Alarm:
	{
		strTips = QObject::tr("请先进行异常断电恢复");
		break;
	}
	case EagleLog_M_Invalid_Task_Name_Alarm:
	{
		strTips = QObject::tr("非法任务名称（仅支持字母，数字，下划线）");
		break;
	}

	default:
		break;
	}

	return strTips;
}

// 算开始结束时间戳 [11/28/2017 xystar]
void SimuReplayContext::generate_time_map()
{
	EagleData_CcdRecord *_ccd = get_related_ccd_rec();
	long current_index = 0;
	unsigned char head_buf[512];

	FrameHead head;
	//		FRAME_TIME frame_time;
	EagleData_Read512InsideFrame(_ccd->record_id, _ccd->id, 0, 0, (char*)head_buf);
	EagleData_AnalyzeFrameHead((const char*)head_buf, &head);
	start_frame_time = (head.h * 3600 + head.m * 60 + head.s) * 1000 + head.ms;
	start_index_head_index = ((unsigned int)head.frame_index_high) << 16 | head.frame_index_low;
	EagleData_Read512InsideFrame(_ccd->record_id, _ccd->id, _ccd->frame_number - 3, 0, (char*)head_buf);
	EagleData_AnalyzeFrameHead((const char*)head_buf, &head);
	end_frame_time = (head.h * 3600 + head.m * 60 + head.s) * 1000 + head.ms;
}
